package com.mikepenz.interactor.imp;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.mikepenz.bean.RequestBean;
import com.mikepenz.interactor.ScanInteractor;
import com.mikepenz.presenter.listener.RequestListener;
import com.mikepenz.utils.CookicUntil;
import com.mikepenz.utils.MethodCode;
import com.mikepenz.utils.MethodType;
import com.quickdv.helper.NetWorkImp;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2015/10/9.
 */
public class ScanInteractorImp extends NetWorkImp implements ScanInteractor {

    RequestListener<JSONObject> listener;
    public ScanInteractorImp(Context c) {
        super(c);
    }

    public ScanInteractorImp(Context c, RequestListener<JSONObject> listener){
        this(c);
        this.listener = listener;
    }

    @Override
    protected void onRequestSuccess(Object tag, JSONObject result) {
        JSONObject response = result.getJSONObject(MethodCode.responseContent_key);
        int statue = response.getInteger(MethodCode.responseStatue);

        if (statue == (MethodCode.NetState.SUCCESS.getValue())) {
            listener.Success(tag.toString(), response.getJSONObject(MethodCode.OBJECT));
        }else if(statue == (MethodCode.CardStatus.ERROR.getValue())){
            listener.Error(tag.toString(),"卡片不可用");
        }else if(statue == (MethodCode.CardStatus.INUSE.getValue())){
            listener.Error(tag.toString(),"卡片已与其他用户绑定");
        }
    }

    @Override
    protected void onRequestExcption(VolleyError error) {
        listener.Exception("", error);
    }

    @Override
    public void bindCard(String userId, String cardId) {
        RequestBean bean = new RequestBean();
        Map<String,Object> params = new HashMap<>();
        params.put("code",cardId);
        bean.setRequestParams(params);
        bean.setUserSession(userId);
        bean.setRequestCode(CookicUntil.getUser().getUserId());
        bean.setRequestCode(MethodType.BINDING_SERVER);
        addFastJsonQueue(Request.Method.POST, MethodType.BINDING_SERVER, (JSONObject) JSON.toJSON(bean));
        startRequest();
    }
}
